// pages2/pages/index/microChat/index.js
const $api = require('../../../../request/api').API;
const app = getApp();
import Utils from '../../../../utils/util';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    beeperId: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      beeperId: options.beeperId
    });
    this.getList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  async getList() {
    let res = await $api.getMicroChatList({
      beeperId: this.data.beeperId
    });
    let list = res.data.data.map(item => {
      return {
        ...item,
        flag: false
      };
    });
    this.setData({
      list
    });
  },

  setInputName(e) {
    const value = e.detail.value;
    const id = e.target.dataset.id;
    let list = this.data.list.map(item => {
      if (item.id === id) {
        return {
          ...item,
          chatName: value
        };
      }
      return item;
    });
    this.setData({
      list
    });
  },

  async editUser(e) {
    let event = e.currentTarget.dataset;
    let list = this.data.list.map(item => {
      if (item.id === event.id) {
        return {
          ...item,
          flag: !item.flag
        };
      }
      return item;
    });
    this.setData({
      list
    });
    if (event.item.flag === true) {
      let res = await $api.editMicroChatFriend({
        beeperId: this.data.beeperId,
        defaultFlag: 0,
        chatName: event.item.chatName,
        chatBeeperId: event.item.chatBeeperId,
        id: event.id
      });
      if (res.data.status == 0) {
        wx.showToast({
          title: '操作成功'
        });
        this.getList();
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'error'
        });
      }
    }
  },

  async delUser(e) {
    const { id } = e.currentTarget.dataset;
    const { item } = e.currentTarget.dataset;
    wx.showModal({
      title: '提示',
      content: '确定要删除该好友吗？',
      success: async res => {
        if (res.confirm) {
          try {
            let res_d = await $api.delMicroChatFriend({
              beeperId: this.data.beeperId,
              chatBeeperId: item.chatBeeperId,
              id: item.id
            });
            if (res_d.data.data) {
              wx.showToast({
                title: '操作成功'
              });
              this.getList();
            } else {
              wx.showToast({
                title: res_d.data.msg,
                icon: 'error'
              });
            }
          } catch (error) {
            console.error('删除好友失败:', error);
          }
        } else if (res.cancel) {
        }
      }
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    let id = this.data.beeperId;
    let startTime = Utils.formatDate(new Date());
    return {
      title: '快来成为微聊好友',
      path: '/subcontract/equipment/pages/addFriendShare/index?beeperId=' + id + '&startTime=' + startTime, // 他人通过卡片进入小程序的路径，可以在后面拼接URL的形式带参数
      imageUrl: 'https://pic.kanglailab.com/common/beeper.png' // 分享出去的图片，默认为当前页面的截图。图片路径可以是本地文件路径或者网络图片路径。支持PNG及JPG。
    };
  },

  onPullDownRefresh: function () {
    this.getList();
  }
});
